const express = require('express')
const app = express()
const port = 9013
const cors=require('cors')
const {env}=require('./dataBase')




//数据库每小时连接一次
// const env = {
//     host: "111.231.22.97",
//     user: "cqbase",
//     password: "MZM5j38RNCjdPRP7",
//     database: "cqbase", //数据库名字
//     multipleStatements: true //允许多条sql语句查询
// }
// const env = {
//     host: "111.231.22.97",
//     user: "cqformal",
//     password: "bWEBYiBeMFJD2XA3",
//     database: "cqformal", //数据库名字
//     multipleStatements: true //允许多条sql语句查询
// }
const mysql=require('mysql')
var timer;
const connect = () => {
  //每小时连接一次
  timer = setInterval(() => {
    let connection = mysql.createConnection(env);
    connection.connect();
  }, 3600000);
};
connect();


//使用express json
app.use(express.json())
app.use(cors())

//登录、注册
const login = require('./login/index')
app.use('/api', login)

//状态
const { status401 } = require('./status/index')
//校验token
const { verifyToken,verifyArr } = require('./util/index')

//首页
const home = require('./home/index')
app.use('/home', home)
//轮播
const carousel=require('./carousel/index')
app.use('/car',carousel)
//列表
const info=require('./info/index')
app.use('/info',info)
//比赛
const match=require('./match/index')
app.use('/mt',match)
//校验
app.use(verifyArr, async (req, res, next) => {
    if (!req.headers.authorization) {
        status401(res, { msg: '登录过期,请重新登录' })
    }
    else {
        const status = await verifyToken(req.headers.authorization)
        if (status == 0) {
            status401(res, { msg: '登录过期,请重新登录' })
        }
        else{
            next()
        }
    }
})





app.listen(port, () => {
    console.log('cq back is run now !')
})